from django.shortcuts import render
from django.views import View
from contents.models import ContentCategory
from django.db import connection
from utils.my_category import get_categories


class IndexView(View):
    """主页"""

    def get(self, request):
        # 商品频道分类
        categories = get_categories()
        # 5. 拼接广告数据
        contents = {}
        content_category = ContentCategory.objects.all()
        for cat in content_category:
            contents[cat.key] = cat.subs.filter(status=True).order_by('sequence')
        # 查看执行原生sql语句，有助于优化
        # for i in connection.queries:
        #     print(i)
        return render(request, "index.html", {'categories': categories, 'contents': contents})
